package leetcode._08_dynic;

import leetcode._05_二叉树.TreeNode;
import org.junit.Test;

import java.util.HashMap;

import static leetcode._05_二叉树.Util.arrToTree;

/**
 * @author pppppp
 * @date 2022/3/31 13:46
 * 小偷又发现了一个新的可行窃的地区。这个地区只有一个入口，我们称之为 root 。
 * 除了 root 之外，每栋房子有且只有一个“父“房子与之相连。一番侦察之后，
 * 聪明的小偷意识到“这个地方的所有房屋的排列类似于一棵二叉树”。
 * 如果 两个直接相连的房子在同一天晚上被打劫 ，房屋将自动报警。
 * 给定二叉树的 root 。返回 在不触动警报的情况下 ，小偷能够盗取的最高金额 。
 * <p>
 * 示例 1:
 * 输入: root = [3,2,3,null,3,null,1]
 * 输出: 7
 * 解释: 小偷一晚能够盗取的最高金额 3 + 3 + 1 = 7
 * <p>
 * 示例 2:
 * 输入: root = [3,4,5,1,3,null,1]
 * 输出: 9
 * 解释: 小偷一晚能够盗取的最高金额 4 + 5 = 9
 * <p>
 * 提示：
 * 树的节点数在 [1, 104] 范围内
 * 0 <= Node.val <= 104
 */
public class _337_打家劫舍III {

    @Test
    public void T_0() {
        Integer[][] nums = {
                {79, 99, 77, null, null, null, 69, null, 60, 53, null, 73, 11},
                {79, 99, 77, null, null, null, 69, null, 60, 53, null, 73, 11, null, null, null, 62, 27, 62, null, null, 98, 50, null, null, 90, 48, 82, null, null, null, 55, 64, null, null, 73, 56, 6, 47, null, 93, null, null, 75, 44, 30, 82, null, null, null, null, null, null, 57, 36, 89, 42, null, null, 76, 10, null, null, null, null, null, 32, 4, 18, null, null, 1, 7, null, null, 42, 64, null, null, 39, 76, null, null, 6, null, 66, 8, 96, 91, 38, 38, null, null, null, null, 74, 42, null, null, null, 10, 40, 5, null, null, null, null, 28, 8, 24, 47, null, null, null, 17, 36, 50, 19, 63, 33, 89, null, null, null, null, null, null, null, null, 94, 72, null, null, 79, 25, null, null, 51, null, 70, 84, 43, null, 64, 35, null, null, null, null, 40, 78, null, null, 35, 42, 98, 96, null, null, 82, 26, null, null, null, null, 48, 91, null, null, 35, 93, 86, 42, null, null, null, null, 0, 61, null, null, 67, null, 53, 48, null, null, 82, 30, null, 97, null, null, null, 1, null, null},
                {3, 2, 3, null, 3, null, 1}, {3, 4, 5, 1, 3, null, 1}};
        int[] ans = {123, 123, 7, 9};
        for (int i = 2; i < nums.length; i++) {
            System.out.println(rob(arrToTree(nums[i])) == ans[i]);
            // System.out.println(rob_1(arrToTree(nums[i])) == ans[i]);
        }
    }

    /*1.gf 优化 */
    public int rob_1(TreeNode root) {
        int[] rootStatus = dfs(root);
        return Math.max(rootStatus[0], rootStatus[1]);
    }

    public int[] dfs(TreeNode node) {
        if (node == null) {
            return new int[]{0, 0};
        }
        int[] l = dfs(node.left);
        int[] r = dfs(node.right);
        int selected = node.val + l[1] + r[1];
        int notSelected = Math.max(l[0], l[1]) + Math.max(r[0], r[1]);
        return new int[]{selected, notSelected};
    }

    /*记忆树 超时了*/
    HashMap<TreeNode, Integer> map = new HashMap<>();

    public int rob(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int selected = 0;
        if (root.left != null && root.right != null) {
            selected = map.getOrDefault(root.left.left, rob(root.left.left)) + map.getOrDefault(root.left.right, rob(root.left.right))
                    + map.getOrDefault(root.right.left, rob(root.right.left)) + map.getOrDefault(root.right.right, rob(root.right.right));
        } else if (root.left != null) {
            selected = map.getOrDefault(root.left.left, rob(root.left.left)) + map.getOrDefault(root.left.right, rob(root.left.right));
        } else if (root.right != null) {
            selected = map.getOrDefault(root.right.left, rob(root.right.left)) + map.getOrDefault(root.right.right, rob(root.right.right));
        }
        selected += root.val;
        int leftV = map.getOrDefault(root.left, rob(root.left));
        int rightV = map.getOrDefault(root.right, rob(root.right));
        int noSelected = leftV + rightV;
        map.put(root, Math.max(selected, noSelected));
        return Math.max(selected, noSelected);
    }
}
